package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClassInformation;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.qo.ClassInfoQuery;
import cn.wolfcode.crm.service.IClassInfomationService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classInformation")
public class ClassInformationController {

    @Autowired
    private IClassInfomationService classInfomationService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ClassInfoQuery qo) {
        //根据高级查询的qo 查询出对应的结果 返回 实现分页
        PageInfo<ClassInformation> result = classInfomationService.query(qo);
        //同时 查询出所有员工对象 共享给前端 作为班主任的下拉框信息
        List<Employee> teachers = classInfomationService.selectAllTeacherName();
        model.addAttribute("result", result);
        model.addAttribute("teachers", teachers);
        return "/class/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ClassInformation classInformation, Long teacherId) {
        Employee employee = new Employee();
        employee.setId(teacherId);
        classInformation.setTeacher(employee);
        if (classInformation.getId() == null) {
            classInfomationService.insert(classInformation);
        } else {
            classInfomationService.updateByPrimaryKey(classInformation);
        }
        return new JsonResult("新增成功", true);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        classInfomationService.deleteByPrimaryKey(id);
        return new JsonResult("删除成功", true);
    }

}
